﻿/*
 * Created by SharpDevelop.
 * User: cszhangyi
 * Date: 2016/4/8
 * Time: 16:10
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Globalization;

using Newtonsoft.Json;
using YMTSeller.Model;
using YMTSeller.Model.SubModel;

namespace YMTSeller
{
	/// <summary>
	/// Description of ActivityHandler.
	/// </summary>
	public class ActivityHandler
	{
		YMTRequest request = new YMTRequest();
		Random r = new Random();
		
		UserHandler user = new UserHandler();
		Dictionary<string, string> userInfoDictionary = null;
		List<ActivityInfoModel> activityValid = new List<ActivityInfoModel>();
		
		private string accessToken = "";
		private string userId = "";
		private DateTime dateTime;
		
		public ActivityHandler(string accessToken, string userId, DateTime dateTime)
		{
			this.accessToken = accessToken;
			this.userId = userId;
			this.dateTime = dateTime;
			
			userInfoDictionary = user.getUserInfo(accessToken, userId);
		}
		
		public string createActivity(DateTime dateTime)
		{
			string url = "http://seller.app.ymatou.com/api/activity/CreateActivity_V2";
			
			var model = new CreateActivityRequestModel();
			
			if (userInfoDictionary["Msg"].Equals("操作成功.") && userInfoDictionary["Status"].Equals("200"))
			{
				int countryId = Int16.Parse(userInfoDictionary["CountryId"]);
				MarketsResponseModel marketsResponseModel = this.ListHistoryMarkets(this.accessToken, this.userId, countryId);
				
				DateTime startTime = dateTime.AddMinutes(2);
				DateTime endTime = startTime.AddHours(2);
				
				model.AccessToken = this.accessToken;
				model.ActivityContent = "自动发布直播~~~~~自动发布直播~~~";
				model.ActivityName = marketsResponseModel.MarketName;
				model.App = "seller";
				model.AppName = "Seller";
				model.ClientId = "43a7b60d2fbcb729ed73d1534972d747";
				model.ClientType = 1;
				model.ClientUserId = long.Parse(userId);
				model.CountryId = int.Parse(userInfoDictionary["CountryId"]);
				model.DelType = 2;
				model.StartTime = startTime.ToString("yyyy-MM-dd HH:mm:ss");
				model.EndTime = endTime.ToString("yyyy-MM-dd HH:mm:ss");
				model.Guid = "5F58DCEF-D449-46FF-81EF-02B569B9B260";
				model.MarketId = int.Parse(marketsResponseModel.MarketId);
				model.Os = "iPhone8,4";
				model.PicUrl = marketsResponseModel.MarketPic;
				model.Position = "中国上海市闸北区大宁路街道共和新路2596号";
				model.Sys = "9.3";
				model.UserId = this.userId;
				model.Version = "2.5.1";
				model.Wifi = 0;
				model.DeviceToken = "623ee5d7e41fd73213df948b9cafa924d4dedc32375a06af7c02eb37c03d5893";
				
				string val = request.post(url, model.toString(), System.Text.Encoding.GetEncoding("utf-8"));
				var responseModel = (CreateActivity_V2ResponseModel) JsonConvert.DeserializeObject(val, typeof(CreateActivity_V2ResponseModel));
				
				return responseModel.Msg;
			}
			else
			{
				return "创建直播失败，" + userInfoDictionary["Msg"];
			}
		}
		
		public string closeActivity()
		{
			activityValid.Clear();
			this.GetActivityListBySeller();
			
			if (activityValid.Count == 0)
				return "没有进行的直播～～";
			else {
				string url = "http://seller.app.ymatou.com/api/activity/CloseActivity";
			
				var model = new CloseActivityRequestModel();
				
				if (userInfoDictionary["Msg"].Equals("操作成功.") && userInfoDictionary["Status"].Equals("200"))
				{
					model.AccessToken = this.accessToken;
					model.ActivityId = 1;
					model.App = "seller";
					model.AppName = "Seller";
					model.ClientId = "43a7b60d2fbcb729ed73d1534972d747";
					model.ClientType = 1;
					model.ClientUserId = long.Parse(userId);
					model.Guid = "5F58DCEF-D449-46FF-81EF-02B569B9B260";
					model.Os = "iPhone8,4";
					model.Sys = "9.3";
					model.UserId = this.userId;
					model.Version = "2.5.1";
					model.Wifi = 0;
					model.DeviceToken = "623ee5d7e41fd73213df948b9cafa924d4dedc32375a06af7c02eb37c03d5893";
					CloseActivityResponseModel responseModel = null;
					foreach(ActivityInfoModel activityInfoModel in activityValid)
					{
						model.ActivityId = activityInfoModel.ActivityId;
						string val = request.post(url, model.toString(), System.Text.Encoding.GetEncoding("utf-8"));
						
						responseModel = (CloseActivityResponseModel) JsonConvert.DeserializeObject(val, typeof(CloseActivityResponseModel));
					}	
					return responseModel.Msg;
				}
				else
				{
					return "结束直播失败";
				}
			}
		}
		
		public string GetActivityListBySeller()
		{
			string url = "http://seller.app.ymatou.com/api/Activity/GetActivityListBySeller";
			
			var model = new GetActivityListBySellerRequestModel();
			
			if (userInfoDictionary["Msg"].Equals("操作成功.") && userInfoDictionary["Status"].Equals("200"))
			{
				int countryId = Int16.Parse(userInfoDictionary["CountryId"]);
				MarketsResponseModel marketsResponseModel = this.ListHistoryMarkets(accessToken, userId, countryId);
				
				DateTime startTime = DateTime.Now.AddMinutes(2);
				DateTime endTime = startTime.AddHours(2);
				
				model.AccessToken = this.accessToken;
				model.App = "seller";
				model.AppName = "Seller";
				model.ClientId = "43a7b60d2fbcb729ed73d1534972d747";
				model.ClientType = 1;
				model.ClientUserId = long.Parse(this.userId);
				model.Guid = "5F58DCEF-D449-46FF-81EF-02B569B9B260";
				model.Os = "iPhone8,4";
				model.Seller = userInfoDictionary["NickName"];
				model.Sys = "9.3";
				model.UserId = this.userId;
				model.Version = "2.5.1";
				model.Wifi = 0;
				model.DeviceToken = "623ee5d7e41fd73213df948b9cafa924d4dedc32375a06af7c02eb37c03d5893";
				
				string val = request.get(url, model.toString(), System.Text.Encoding.GetEncoding("utf-8"));
				var responseModel = (GetActivityListBySellerResponseModel) JsonConvert.DeserializeObject(val, typeof(GetActivityListBySellerResponseModel));
				
				int count_activing = 0;
				string msg = "";
				
				foreach (ActivityInfoModel activityInfoModel in responseModel.Result.ActivityList)
				{
					if (activityInfoModel.ActivityState == 0)
					{
						count_activing += 1;
						this.activityValid.Add(activityInfoModel);
						msg += "有" + count_activing + "场直播正在进行，直播id为" + activityInfoModel.ActivityId + "\n"; 
					}
				}
				
				if (count_activing == 0)
					return "当前没有正在进行的直播";
				
				return msg;
			}
			else
			{
				return "获取直播信息失败，" + userInfoDictionary["Msg"];
			}
		}
		
		public int GetTopActivityBySeller()
		{
			string url = "http://seller.app.ymatou.com/api/Activity/GetActivityListBySeller";
			
			var model = new GetActivityListBySellerRequestModel();
			
			if (userInfoDictionary["Msg"].Equals("操作成功.") && userInfoDictionary["Status"].Equals("200"))
			{
				int countryId = Int16.Parse(userInfoDictionary["CountryId"]);
				MarketsResponseModel marketsResponseModel = this.ListHistoryMarkets(accessToken, userId, countryId);
				
				DateTime startTime = DateTime.Now.AddMinutes(2);
				DateTime endTime = startTime.AddHours(2);
				
				model.AccessToken = this.accessToken;
				model.App = "seller";
				model.AppName = "Seller";
				model.ClientId = "43a7b60d2fbcb729ed73d1534972d747";
				model.ClientType = 1;
				model.ClientUserId = long.Parse(this.userId);
				model.Guid = "5F58DCEF-D449-46FF-81EF-02B569B9B260";
				model.Os = "iPhone8,4";
				model.Seller = userInfoDictionary["NickName"];
				model.Sys = "9.3";
				model.UserId = this.userId;
				model.Version = "2.5.1";
				model.Wifi = 0;
				model.DeviceToken = "623ee5d7e41fd73213df948b9cafa924d4dedc32375a06af7c02eb37c03d5893";
				
				string val = request.get(url, model.toString(), System.Text.Encoding.GetEncoding("utf-8"));
				var responseModel = (GetActivityListBySellerResponseModel) JsonConvert.DeserializeObject(val, typeof(GetActivityListBySellerResponseModel));
				
				return responseModel.Result.ActivityList[0].ActivityId;
			}
			else
			{
				return 0;
			}
		}
		
		private MarketsResponseModel ListHistoryMarkets(string accessToken, string userId, int countryId)
		{
			string url = "http://seller.app.ymatou.com/api/Activity/ListHistoryMarkets";
			
			ListHistoryMarketsRequestModel model = new ListHistoryMarketsRequestModel();
			
			model.AccessToken = this.accessToken;
			model.AppName = "Seller";
			model.ClientId = "43a7b60d2fbcb729ed73d1534972d747";
			model.ClientType = 1;
			model.ClientUserId = this.userId;
			model.CountryId = countryId;
			model.DeviceToken = "623ee5d7e41fd73213df948b9cafa924d4dedc32375a06af7c02eb37c03d5893";
			model.Guid = "5F58DCEF-D449-46FF-81EF-02B569B9B260";
			model.SellerId = this.userId;
			model.UserId = this.userId;
			model.Wifi = 0;
			model.App = "Seller";
			model.Os = "iPhone8,4";
			model.Sys = "9.3";
			model.Version = "2.5.1";
			
			string val = request.get(url, model.toString(), System.Text.Encoding.GetEncoding("utf-8"));
				
			ListHistoryMarketsResponseModel responseModel = (ListHistoryMarketsResponseModel) JsonConvert.DeserializeObject(val, typeof(ListHistoryMarketsResponseModel));
			
			if (responseModel.Msg.Equals("操作成功") && responseModel.Status == 200)
			{
				if (responseModel.Result.Count > 0)
				{
					int i = r.Next(0, responseModel.Result.Count - 1);
					return responseModel.Result[i];
				}
				else 
					return null;
			}
			else
				return null;
		}
	}
}
